package com.example.hcbmsm.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.example.hcbmsm.domain.Op;
import com.example.hcbmsm.domain.OpExample;
import com.example.hcbmsm.mapper.OpMapper;
import com.example.hcbmsm.service.OpService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.example.hcbmsm.util.JavaWebToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/10/30 16:12
 */
@Service
public class OpServiceImpl implements OpService {
    @Autowired
    private OpMapper opMapper;

    @Override
    public Map add(String token, Op op) {
        int opId = opMapper.selectMaxId() + 1;
        if (!"".equals(token) && token != null){
            Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
            op.setPatId(new Long(tokenInfo.get("patId").asString()));
        }
        op.setOpId((long) opId);
        if (opMapper.insertSelective(op) == 1) {
            return HCBMSMResult.SUCCESS(opId);
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map findAllOp(String token) {
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        Long patId = new Long(tokenInfo.get("patId").asString());
        OpExample opExample = new OpExample();
        opExample.createCriteria().andPatIdEqualTo(patId);
        opExample.setOrderByClause("create_time desc");
        return HCBMSMResult.SUCCESS(opMapper.selectByExample(opExample));
    }

    @Override
    public Map renew(Op op) {
        if (opMapper.updateByPrimaryKeySelective(op) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map remove(Long opId) {
        if (opMapper.deleteByPrimaryKey(opId) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }
}
